﻿package components {
	
	import com.greensock.TweenLite;
	import com.greensock.easing.Circ;
	import com.greensock.easing.Quart;
	
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	import flash.ui.Mouse;
	import flash.utils.Timer;
	import com.greensock.easing.Linear;
	
	public class Topic extends MovieClip {
		
		private var _cx:Number;
		private var _cy:Number;
		private var _cz:Number;
		public var per:Number;
		
		private var time:int = 0;
		private var maxTime:int = 5;
		
		private var _this:MovieClip;
		
		private var _tf:TextField;
		private var _atf:TextField;
		private var _back:Sprite;
		
		private var _hicolor:Number = 0xcccccc;
		
		public var isDragged:Boolean = false;
		
		private var _root:MovieClip;
		
		public var broadcast:Broadcast;

		public function Topic(parentMC:MovieClip,broadcast:Broadcast) {
			
			this.broadcast = broadcast;
			_root = parentMC;
			_this = this;
			
			_tf = new TextField();
			_tf.autoSize = TextFieldAutoSize.LEFT;
			_tf.selectable = false;
			_tf.text = broadcast.theme;
			_tf.x = 10;
			_tf.y = - 10;
			addChild(_tf);
			
			_atf = new TextField();
			_atf.autoSize = TextFieldAutoSize.LEFT;
			_atf.selectable = false;
			_atf.text = broadcast.author;
			_atf.x = 10;
			_atf.y = 0;
			
			var format:TextFormat = new TextFormat();
			format.font = "Arial";
			//format.bold = true;
			format.color = 0x0099ff;
			format.size = 7;
			_atf.defaultTextFormat = format;
			_atf.setTextFormat(format);
			addChild(_atf);
			
			// create the back
			_back = new Sprite();
			_back.graphics.beginFill(_hicolor, 0);
			_back.graphics.lineStyle(0, _hicolor);
			_back.graphics.drawRect(0, 0, _tf.textWidth+40, _tf.textHeight+10);
			_back.graphics.endFill();
			addChildAt(_back, 0);
			_back.x = -10;//-( _tf.textWidth/2 ) - 10;
			_back.y = -10;//-( _tf.textHeight/2 ) - 2;
			_back.visible = false;
			
			addEventListener(MouseEvent.MOUSE_OUT, mouseOutHandler);
			addEventListener(MouseEvent.MOUSE_OVER, mouseOverHandler);
			
			addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
		    addEventListener(MouseEvent.MOUSE_UP, mouseReleasedHandler);
		}
		
		private function mouseOverHandler( e:MouseEvent ):void {
			_back.visible = true;
			//_tf.textColor = _hicolor;
			//_active = true;
		}
		
		private function mouseOutHandler( e:MouseEvent ):void {
			_back.visible = false;
			//_tf.textColor = _color;
			//_active = false;
		}
		
		private function mouseDownHandler(e:MouseEvent):void {
			startDrag();
			//Mouse.hide();
			isDragged = true;
			TweenLite.to(this, 2, {scaleX:2, scaleY:2, alpha:1});
			_root.setActiveElement(this);
		}

		private function mouseReleasedHandler(e:MouseEvent):void {
   			stopDrag();
			//Mouse.show();
			//var dx:Number = _cx*per;
			//var dy:Number = _cy*per;

			//TweenLite.to(this, 5, {x:dx, y:dy, scaleX:per, scaleY:per, alpha:per/2});
			//var obj = e.target;
			var dx:Number = _cx*per;
			var dy:Number = _cy*per;
			var tx:Number;
			var ty:Number;
			
			tx = (x + dx)/5*time;
			ty = (y + dy)/5*time;
			
			
			//TweenLite.to(_this, 5, {x:0, y:0});
			//moveBack();
			
			var minuteTimer:Timer = new Timer(1000, 5	);

			minuteTimer.addEventListener(TimerEvent.TIMER, function():void {
				
				var dx:Number = _cx*per;
				var dy:Number = _cy*per;
				var tx:Number;
				var ty:Number;
				
				tx = (x + dx)/5*time;
				ty = (y + dy)/5*time;
				//tx = (x + dx)/2;
				//ty = (y + dy)/2;
				
				//x = tx;
				//y = ty;
				
				TweenLite.to(_this, 2, {x:0, y:0, scaleX:per, scaleY:per, alpha:per/2});
				time++;
			});
			
			minuteTimer.addEventListener(TimerEvent.TIMER_COMPLETE, function():void {
				
				/*var dx:Number = _cx*per;
				var dy:Number = _cy*per;
				var tx:Number;
				var ty:Number;
				
				tx = (x + dx)/5*time;
				ty = (y + dy)/5*time;*/
				
				/*TweenLite.to(_this, 5, {x:tx, y:ty, scaleX:1, scaleY:1, alpha:1/2, 
								onComplete: function():void {
									isDragged = false;
								}
				});*/
				isDragged = false;
				time = 1;
			}); 
			
			//minuteTimer.start(); 
									
			
			//this.dropTarget.parent.mouseUpHandler(e);
			//e.get
			//e.target.dropTarget.parent.mouseUpHandler(e);
			//_root.setActiveElement(null);
		}
		
		public function moveBack():void {
			
				time++;
			
				var dx:Number = _cx*per;
				var dy:Number = _cy*per;
				var tx:Number;
				var ty:Number;
				
				//tx = (x + dx)/maxTime*time;
				//ty = (y + dy)/maxTime*time;
				//tx = (x + dx)*time/(time+1);
				//ty = (y + dy)*time/(time+1);
				tx = (x + dx)/2;
				ty = (y + dy)/2;
				
				TweenLite.to(_this, 1/time, {x:tx, y:ty, scaleX:per, scaleY:per, alpha:per/2, ease:Linear.easeNone}); //, onComplete: moveBack});
				var minuteTimer:Timer = new Timer(1000/time - 150, 1);
				minuteTimer.addEventListener(TimerEvent.TIMER_COMPLETE, function():void {
					moveBack();
				});
				if(time <= maxTime) {
					minuteTimer.start();
				} else {
					isDragged = false;
					time = 0;
				}
		}
		
		public function remove(index:Number):void {
			
			TweenLite.to(this, 3, {alpha:0, 
				onComplete: function():void{
					_root.removeChild(_root.mcList[index]);
					_root.mcList.splice(index,1);
				}
			});
			
			//_root.removeChild(_root.mcList[index]);
			//_root.mcList.splice(index,1);
			
			/*var t = new Tween(this, "alpha", Regular.easeIn, alpha, 0, 3, true);
			t.addEventListener(TweenEvent.MOTION_FINISH, function(e:TweenEvent) {
							   		_root.removeChild(_root.mcList[index]);
									_root.mcList.splice(index,1);
									
							   });
			*/
			//_root.removeChild(this);
		}
		
		public function set cx(n:Number):void{
			_cx = n;
		}
		public function set cy(n:Number):void{
			_cy = n;
		}
		public function set cz(n:Number):void{
			_cz = n;
		}
		public function get cx():Number{
			return _cx;
		}
		public function get cy():Number{
			return _cy;
		}
		public function get cz():Number{
			return _cz;
		}
	}
	
}
